local bind = require('utils.key_bind');

local map_cr = bind.map_cr
local map_cu = bind.map_cu
local map_args = bind.map_args
local map_cmd = bind.map_cmd

local key_mapping = {
    -- Moving
    -- ['n|J'] = map_cmd('5j'),
    -- ['n|K'] = map_cmd('5k'),
    -- ['v|J'] = map_cmd('5j'),
    -- ['v|K'] = map_cmd('5k'),
    ['n|j'] = map_cmd('<plug>(faster_move_j)'):with_silent(),
    ['n|k'] = map_cmd('<plug>(faster_move_k)'):with_silent(),

    ['n|,'] = map_cmd(':'),
    ['i|jj'] = map_cmd('<Esc>'),

    -- Terminal
    -- ['t|<Esc>'] = map_cmd('<C-\\><C-n>'):with_noremap(),
    ['t|<c-q>'] = map_cmd('<C-\\><C-n>'):with_noremap(),

    -- Quick
    ['n|<C-s>'] = map_cr('w'),
    ['i|<C-s>'] = map_cmd('<Esc>:w<ESC>'),
    ['i|<C-a>'] = map_cmd('<Esc>^i'):with_noremap(),
    ['i|<C-e>'] = map_cmd('<Esc>$i'):with_noremap(),
    ['n|<C-a>'] = map_cmd('^i'):with_noremap(),
    ['n|<C-e>'] = map_cmd('$a'):with_noremap(),

    ['n|U'] = map_cmd('<C-r>'):with_noremap(),

    -- Command
    ['c|<C-a>'] = map_cmd('<Home>'):with_noremap(),
    ['c|<C-e>'] = map_cmd('<End>'):with_noremap(),
    ['c|<C-p>'] = map_cmd('<Up>'):with_noremap(),
    ['c|<C-n>'] = map_cmd('<Down>'):with_noremap(),
    ['c|<C-b>'] = map_cmd('<Left>'):with_noremap(),
    ['c|<C-f>'] = map_cmd('<Right>'):with_noremap(),
    ["c|<C-d>"] = map_cmd('<Del>'):with_noremap(),
    ["c|<C-h>"] = map_cmd('<BS>'):with_noremap(),
    ["c|<C-t>"] = map_cmd([[<C-R>=expand("%:p:h") . "/" <CR>]]):with_noremap(),

    -- Split
    ['n|sl'] = map_cmd(':set splitright<CR>:vsplit<CR>'),
    ['n|sh'] = map_cmd(':set nosplitright<CR>:vsplit<CR>'),
    ['n|sk'] = map_cmd(':set nosplitbelow<CR>:split<CR>'),
    ['n|sj'] = map_cmd(':set splitbelow<CR>:split<CR>'),
    ['n|sV'] = map_cmd('<C-w>t<C-w>H'),
    ['n|sH'] = map_cmd('<C-w>t<C-w>K'),

    -- Window
    -- ['n|<C-h>'] = map_cmd('<C-w>h'):with_noremap(),
    -- ['n|<C-l>'] = map_cmd('<C-w>l'):with_noremap(),
    -- ['n|<C-j>'] = map_cmd('<C-w>j'):with_noremap(),
    -- ['n|<C-k>'] = map_cmd('<C-w>k'):with_noremap(),
    ['n|_'] = map_cr('res -5'),
    ['n|+'] = map_cr('res +5'),
    ['n|-'] = map_cr('vertical :res -5'),
    ['n|='] = map_cr('vertical :res +5'),

    -- Tab
    ['|tt'] = map_cr('tabe'),
    ['|tj'] = map_cr('-tabnext'),
    ['|tk'] = map_cr('+tabnext'),

    -- Buffer
    ['|<leader>j'] = map_cr('bn'),
    ['|<leader>k'] = map_cr('bp'),

    -- LSP SAGA
    -- ["n|<C-f>"] = map_cmd(
    --     "<cmd>lua require('lspsaga.action').smart_scroll_with_saga(1)<CR>"):with_silent()
    --     :with_noremap():with_nowait(),
    -- ["n|<C-b>"] = map_cmd(
    --     "<cmd>lua require('lspsaga.action').smart_scroll_with_saga(-1)<CR>"):with_silent()
    --     :with_noremap():with_nowait(),
    -- ["n|[e"] = map_cr('Lspsaga diagnostic_jump_next'):with_noremap()
    --     :with_silent(),
    -- ["n|]e"] = map_cr('Lspsaga diagnostic_jump_prev'):with_noremap()
    --     :with_silent(),
    -- ["n|<Leader>d"] = map_cr("Lspsaga hover_doc"):with_noremap():with_silent(),
    -- ["n|sa"] = map_cr("Lspsaga code_action"):with_noremap():with_silent(),
    -- ["v|sa"] = map_cu("Lspsaga range_code_action"):with_noremap():with_silent(),
    -- ["n|gd"] = map_cr('Lspsaga preview_definition'):with_noremap():with_silent(),
    -- ["n|ss"] = map_cr('Lspsaga signature_help'):with_noremap():with_silent(),
    -- ["n|sr"] = map_cr('Lspsaga rename'):with_noremap():with_silent(),
    -- ["n|sf"] = map_cr('Lspsaga lsp_finder'):with_noremap():with_silent(),
    -- ["n|<A-d>"] = map_cu('Lspsaga open_floaterm'):with_noremap():with_silent(),
    -- ["t|<A-d>"] = map_cu([[<C-\><C-n>:Lspsaga close_floaterm<CR>]]):with_noremap()
    --     :with_silent(),
    -- ["n|<Leader>lg"] = map_cu("Lspsaga open_floaterm lazygit"):with_noremap()
    --     :with_silent(),

    -- LSP Trouble
    ['n|<leader>xx'] = map_cr('Trouble'):with_noremap():with_silent(),
    ['n|<leader>xw'] = map_cr('Trouble lsp_workspace_diagnostics'):with_noremap()
        :with_silent(),
    ['n|<leader>xd'] = map_cr('Trouble lsp_document_diagnostics'):with_noremap()
        :with_silent(),
    ['n|<leader>xq'] = map_cr('TroubleToggle quickfix'):with_noremap()
        :with_silent(),
    ['n|<leader>xl'] = map_cr('TroubleToggle loclist'):with_noremap(),

    -- Telescope
    ['n|<leader>fp'] = map_cr('Telescope find_files'):with_noremap():with_silent(),
    ['n|<leader>fg'] = map_cr('Telescope live_grep'):with_noremap():with_silent(),
    ['n|<leader>fb'] = map_cr('Telescope buffers'):with_noremap():with_silent(),
    ['n|<leader>fh'] = map_cr('Telescope help_tags'):with_noremap():with_silent(),
    ['n|<leader>fc'] = map_cr('Telescope git_bcommits'):with_noremap():with_silent(),
    ['n|<leader>fl'] = map_cr('Telescope loclist'):with_noremap():with_silent(),
    ['n|<leader>fq'] = map_cr('Telescope quickfix'):with_noremap():with_silent(),
    ['n|<leader>fm'] = map_cr('Telescope marks'):with_noremap():with_silent(),
    ['n|<leader>fk'] = map_cr('Telescope keymaps'):with_noremap():with_silent(),
    ['n|<leader>fo'] = map_cr('Telescope oldfiles'):with_noremap():with_silent(),
    ['n|<leader>fd'] = map_cr('Telescope dotfiles'):with_noremap():with_silent(),
    ['n|<leader>fa'] = map_args('Telescope live_grep search_dirs='):with_noremap(),

    -- TodoComment
    ['n|<leader>tf'] = map_cr('TodoQuickFix'),
    ['n|<leader>ft'] = map_cr('TodoTelescope'),
    ['n|<leader>tl'] = map_cr('TodoLocList'),
    ['n|<leader>xt'] = map_cr('TodoTrouble'),

    -- Format
    ['n|<leader>ff'] = map_cr('Neoformat'),

    -- Comment
    ['n|<leader>cc'] = map_cmd('<Plug>kommentary_line_default'),
    ['n|<leader>c'] = map_cmd('<Plug>kommentary_motion_default'),
    ['x|<leader>c'] = map_cmd('<Plug>kommentary_visual_default'),

    -- Dashboard
    ['n|<leader>fn'] = map_cr('DashboardNewFile'),
    ['n|<leader>sl'] = map_cr('SessionLoad'),

    -- Git
    ['n|<leader>gg'] = map_cr('LazyGit'):with_noremap():with_silent(),

    -- Tree
    ['n|ge'] = map_cr('NvimTreeToggle'):with_noremap(),
    ['n|<leader>n'] = map_cr('NvimTreeFindFile'):with_noremap(),

    -- Hop
    ['n|sw'] = map_cr('HopWord'):with_noremap(),
    ['n|sc'] = map_cr('HopChar1'):with_noremap(),
    ['n|se'] = map_cr('HopLine'):with_noremap(),

    -- Navigator
    ['n|<C-h>'] = map_cr('lua require("Navigator").left()'):with_noremap(),
    ['n|<C-l>'] = map_cr('lua require("Navigator").right()'):with_noremap(),
    ['n|<C-j>'] = map_cr('lua require("Navigator").down()'):with_noremap(),
    ['n|<C-k>'] = map_cr('lua require("Navigator").up()'):with_noremap(),
    ['n|<C-p>'] = map_cr('lua require("Navigator").previous()'):with_noremap(),

    -- Vista
    ['n|<leader>v'] = map_cu('Vista!!'):with_noremap():with_silent(),

    -- Bufferline
    ['n|<leader>1'] = map_cr('BufferLineGoToBuffer 1'):with_noremap()
        :with_silent(),
    ['n|<leader>2'] = map_cr('BufferLineGoToBuffer 2'):with_noremap()
        :with_silent(),
    ['n|<leader>3'] = map_cr('BufferLineGoToBuffer 3'):with_noremap()
        :with_silent(),
    ['n|<leader>4'] = map_cr('BufferLineGoToBuffer 4'):with_noremap()
        :with_silent(),
    ['n|<leader>5'] = map_cr('BufferLineGoToBuffer 5'):with_noremap()
        :with_silent(),
    ['n|<leader>6'] = map_cr('BufferLineGoToBuffer 6'):with_noremap()
        :with_silent(),
    ['n|<leader>7'] = map_cr('BufferLineGoToBuffer 7'):with_noremap()
        :with_silent(),
    ['n|<leader>8'] = map_cr('BufferLineGoToBuffer 8'):with_noremap()
        :with_silent(),
    ['n|<leader>9'] = map_cr('BufferLineGoToBuffer 9'):with_noremap()
        :with_silent(),

    -- Gitsigh
    ['n|]c'] = map_cr('lua require"gitsigns.actions".next_hunk()'):with_noremap()
        :with_silent(),
    ['n|[c'] = map_cr('lua require"gitsigns.actions".prev_hunk()'):with_noremap()
        :with_silent(),
    ['n|<leader>hs'] = map_cr('lua require"gitsigns".stage_hunk()'):with_noremap(),
    ['v|<leader>hs'] = map_cr(
        'lua require"gitsigns".stage_hunk({vim.fn.line("."), vim.fn.line("v")})'):with_noremap(),
    ['n|<leader>hu'] = map_cr('lua require"gitsigns".undo_stage_hunk()'):with_noremap(),
    ['n|<leader>hr'] = map_cr('lua require"gitsigns".reset_hunk()'):with_noremap(),
    ['v|<leader>hr'] = map_cr(
        'lua require"gitsigns".reset_hunk({vim.fn.line("."), vim.fn.line("v")})'):with_noremap(),
    ['n|<leader>hR'] = map_cr('lua require"gitsigns".reset_buffer()'):with_noremap(),
    ['n|<leader>hp'] = map_cr('lua require"gitsigns".preview_hunk()'):with_noremap(),
    ['n|<leader>hb'] = map_cr('lua require"gitsigns".blame_line(true)'):with_noremap(),
    ['n|<leader>hB'] = map_cr('lua require"gitsigns".stage_buffer()'):with_noremap(),
    ['n|<leader>hU'] = map_cr('lua require"gitsigns".reset_buffer_index()'):with_noremap(),
    ['o|ih'] = map_cu('lua require"gitsigns.actions".select_hunk()'):with_noremap(),
    ['x|ih'] = map_cu('lua require"gitsigns.actions".select_hunk()'):with_noremap(),

    -- Theme
    ['n|<leader>mm'] = map_cr('lua require("material.functions").toggle_style()'),

    -- Treesitter
    ['n|<leader>rr'] = map_cr('write | edit | TSBufEnable highlight')

}

bind.load_mapping(key_mapping)
